package io.mft.as.ar.gateway.util;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.SocketChannel;

import lombok.extern.slf4j.Slf4j;

/**
 * 描述: 
 * 版权: Copyright (c) 2012 
 * 公司: 思迪科技 
 * 作者: 李炜
 * 版本: 1.0 
 * 创建日期: Dec 4, 2013 
 * 创建时间: 11:40:36 AM
 */
@Slf4j
public class SocketHelper
{
	
	
	/**
	 *
	 * @param server   服务器IP地址
	 * @param port     端口
	 * @return
	 */
	public static boolean connectServer(String server, int port)
	{
		SocketChannel channel = null;
		boolean isAlive = false;
		try
		{
			channel = SocketChannel.open();
			SocketAddress address = new InetSocketAddress(server, port);
			channel.configureBlocking(true);
			channel.socket().setSoTimeout(10);
			isAlive = channel.connect(address);
			
		}
		catch (Exception ex)
		{
			log.info(ex.getMessage());
			return false;
		}
		finally
		{
			if (channel != null)
			{
				try
				{
					channel.close();
				}
				catch (IOException e)
				{
					log.info(e.getMessage());
				}
			}
		}
		return isAlive;
	}
	
}
